﻿using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.RoleCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.RoleHandler
{
    public class RoleUpdateCommandHandler : IRequestHandler<RoleUpdateCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Role> roleRepos;

        public RoleUpdateCommandHandler(IBaseRepository<Role> roleRepos)
        {
            this.roleRepos = roleRepos;
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<object>> Handle(RoleUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var getObj = roleRepos.GetAll().FirstOrDefault(x => x.RoleId == request.RoleId);
            getObj.RoleName = request.RoleName;
            getObj.UpdateUser = "张三";
            getObj.UpdateTime = DateTime.Now;
            //var model = mapper.Map<User>(request);
            var obj = roleRepos.Update(getObj);
            if (obj > 0)
            {
                result.Code = APIEnum.Success;
                result.Msg = "修改成功";
                result.Data = obj;
            }
            else
            {
                result.Code = APIEnum.Failed;
                result.Msg = "修改失败";
            }

            return Task.FromResult(result);
        }
    }
}
